package io.wins.vertx.service.impl;

import io.vertx.core.Vertx;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.wins.vertx.entity.Root;
import io.wins.vertx.service.BaseService;
import io.wins.vertx.service.RootService;

public class RootServiceImpl extends BaseService implements RootService {
    public RootServiceImpl(final Vertx vertx) {
        this.vertx = vertx;
    }

    @Override
    public RootService getRootById(final String id, final Message tMessage) {
        return this;
    }

    @Override
    public RootService listRoot(final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "select * from ds_root ORDER BY seq asc");
        this.querySql(sqlMes, this.resultQuery(tMessage, false, null, "Error"));
        return this;
    }

    @Override
    public RootService createRoot(final Root root, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "insert into ds_root (id, parent_id, name, url, type, seq, created_on, updated_on) values(?,?,?,?,?,?,UNIX_TIMESTAMP(),UNIX_TIMESTAMP())")
            .put(PARAMS, new JsonArray()
                .add(root.getId())
                .add(root.getParentId())
                .add(root.getName())
                .add(root.getUrl())
                .add(root.getType())
                .add(root.getSeq()));
        this.insertSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }

    @Override
    public RootService updateRoot(final Root root, final Message tMessage) {
        final JsonObject sqlMes = new JsonObject()
            .put(SQL, "update ds_root set parent_id = ?, name = ?, url = ?, type = ?, seq = ? where id = ?")
            .put(PARAMS, new JsonArray()
                .add(root.getParentId())
                .add(root.getName())
                .add(root.getUrl())
                .add(root.getType())
                .add(root.getSeq())
                .add(root.getId()));
        this.updateSql(sqlMes, this.resultUpdate(tMessage, "Error"));
        return this;
    }
}
